package com.example.lenovo.weiyi;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.Params;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import jcvideoplayer_lib.JCVideoPlayer;
import jcvideoplayer_lib.JCVideoPlayerStandard;
import music.Player;
import share.HttpIP;
import yulanpic.ImagePagerActivity;

import static com.com.ruanmeng.utils.Params.IsPlaying;

/**
 * 新的锻炼内容页面
 */
public class TrainContentActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener {

    @BindView(R.id.videoplayer_traincontent)
    JCVideoPlayerStandard jcVideoPlayerStandard;
    @BindView(R.id.tv_traincontent_title)
    TextView tvTraincontentTitle;
    @BindView(R.id.imv_radio_pic)
    ImageView imvRadioPic;
    @BindView(R.id.imv_radio_play)
    ImageView imvRadioPlay;
    @BindView(R.id.tv_trainc_bz)
    TextView tvTraincBz;
    @BindView(R.id.music_progress)
    SeekBar musicProgress;
    @BindView(R.id.imv_itemc_pic)
    ImageView imvItemcPic;
    @BindView(R.id.tv_itemc_content)
    TextView tvItemcContent;
    @BindView(R.id.activity_train_content)
    LinearLayout activityTrainContent;
    @BindView(R.id.li_train_music)
    LinearLayout liTrainMusic;
    @BindView(R.id.li_train_xj)
    LinearLayout liTrainXj;
    private int page = 1;
    ArrayList<String> Temp_list = new ArrayList<>();
    private Player player;
    private AnimationDrawable animationDrawable;
    private int IsFirst;
    private Timer autoUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_content);
        ButterKnife.bind(this);
        ((TextView) findViewById(R.id.tv_lay_title))
                .setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.tv_lay_title)).setText("锻炼内容");
        LayBack();
        init();
    }

    public void init() {
        imvRadioPic.setImageResource(R.drawable.radio_suspended04);
        Params.IsPlaying = 0;
        Params.IsVideoing = 0;
        Params.IsPausrVideo = 0;
        if (TextUtils.isEmpty(getIntent().getStringExtra("videolink"))) {
            jcVideoPlayerStandard.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(getIntent().getStringExtra("musiclink"))) {
            liTrainMusic.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(getIntent().getStringExtra("pic"))) {
            liTrainXj.setVisibility(View.GONE);
        }
        tvTraincontentTitle.setText(getIntent().getStringExtra("title"));
        tvTraincBz.setMovementMethod(LinkMovementMethod.getInstance());
        tvTraincBz.setText(Html.fromHtml(getIntent().getStringExtra("intor")).toString().trim());
        ImgDataUtil.loadImage(TrainContentActivity.this, HttpIP.Base_IpIMage + getIntent().getStringExtra("pic"), imvItemcPic);

        //关于视频
        JCVideoPlayer.SAVE_PROGRESS = false;////是否缓存播放位置
        jcVideoPlayerStandard.setUp(HttpIP.Base_IpIMage + getIntent().getStringExtra("videolink")
                , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "");
//        jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4"
//                , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "");
        //关于音频
        player = new Player(musicProgress);
        player.mediaPlayer.setOnCompletionListener(this);
        //监控试看结束暂停后  是否按下继续播放按钮
        autoUpdate = new Timer();
        autoUpdate.schedule(new TimerTask() {//定时看  在是看结束后有没有再次点击播放

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (Params.IsPlayVideo == 1) {//点击播放视频  停止音乐
                            if (player != null && player.mediaPlayer != null&&Params.IsPlaying==1) {
//                            if (player != null && player.mediaPlayer != null && player.mediaPlayer.isPlaying()) {
                                IsPlaying = 0;
                                imvRadioPlay.setBackgroundResource(R.drawable.radio_suspended);
                                player.mediaPlayer.pause();
                                stopadmin();
                                imvRadioPic.setImageResource(R.drawable.radio_suspended04);
                            }

                        }

                    }
                });
            }
        }, 0, 500);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        if (IsFirst == 0) {//避免进入的时候走该方法
            IsFirst = 1;
            Params.IsPlaying = 0;
            imvRadioPlay.setBackgroundResource(R.drawable.radio_suspended);
            stopadmin();
            imvRadioPic.setImageResource(R.drawable.radio_suspended04);
        }
    }


    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.stop();
            player = null;
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if (player == null) {
            player = new Player(musicProgress);
        }
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.imv_radio_play:
                if (IsPlaying == 1) {
                    IsPlaying = 0;
                    imvRadioPlay.setBackgroundResource(R.drawable.radio_suspended);
                    player.mediaPlayer.pause();
                    stopadmin();
                    imvRadioPic.setImageResource(R.drawable.radio_suspended04);
                } else {
                    if (Params.IsPlayVideo == 1) {//正在播放视频
                        Params.IsPlayVideo = 0;
                        Params.IsPlayingMusic = 1;//因为点击音频而暂停视频标记
                    }
                    Params.IsPlaying = 1;
                    imvRadioPlay.setBackgroundResource(R.drawable.radio_play);
                    staradmin();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            player.playUrl(HttpIP.Base_IpIMage + getIntent().getStringExtra("musiclink"));
                        }
                    }).start();
                }
                break;
            case R.id.imv_itemc_pic:
                YuLanPic(HttpIP.Base_IpIMage + getIntent().getStringExtra("pic"));
                break;
        }
    }

    //播放动画开始
    public void staradmin() {
        try {
            imvRadioPic.setImageResource(R.drawable.animatuion_pic);
            animationDrawable = (AnimationDrawable) imvRadioPic.getDrawable();
            animationDrawable.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //停止播放动画
    public void stopadmin() {
        try {
            animationDrawable = (AnimationDrawable) imvRadioPic.getDrawable();
            animationDrawable.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //返回事件
    public void LayBack() {
        ((ImageView) findViewById(R.id.imv_lay_back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(R.anim.pre_in, R.anim.pre_out);
            }
        });
    }

    /**
     * 预览图片
     */
    public void YuLanPic(String url) {
        String[] aStrings = new String[1];
        aStrings[0] = url;
        Intent intent1 = new Intent(TrainContentActivity.this,
                ImagePagerActivity.class); //
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent1.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, aStrings);
        intent1.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
        startActivity(intent1);
    }

}
